import { observable, action } from 'mobx';

import AppStore from '../store/appStore';
import UserStore from '../store/userStore';
import {Keys as StorageKeys,Storage} from '../storage';
const TAG = "数据 :: 数据中心DataCenter :: \\";
// 一般为处理对象类型数据的接口key声明
interface DataKeys {
    DCAssets: string,
    DCUser: string,
}

export const Keys:DataKeys = {
    DCAssets: "DCAssets",
    DCUser: "DCUser"
}

type DATA = DCAssets | number | string | boolean | DCUserType;

class DataCenter {
    @observable public App = new AppStore();
    @observable public User = new UserStore();

    // 定义其他全局数据
    @observable public Assets = {}

    constructor(){
        /**
         *  数据中心初始化引擎 1
         */
        this.__User_Core();
    }


    @action.bound
    public setAssets(v: DCAssets){
        this.Assets = v;      
    }

    /**
     * 
     * @param user 
     * 状态数据直接处理模块
     * 
     */
    @action.bound
    private async __User_Core(){
        const user = await Storage.getItem(StorageKeys.user);
        const firstinstall = await Storage.getItem(StorageKeys.firstInstall);
        if(firstinstall == null){
            await Storage.setItem(StorageKeys.firstInstall, true);
        }
        if (user) {
            this.User_SignIn(user);
        }
        this.App_setAppLoading(false);
    }

    @action.bound
    public User_SignIn(user: DCUserType){
        console.log(TAG,"用户登录数据恢复 , User_SignIn")
        Storage.setItem(StorageKeys.user, user);
        this.User.user = {...user};
        this.User.logined = true;
    }

    @action.bound
    public User_SignOut(user: DCUserType){
        Storage.removeItem(StorageKeys.user);
        this.User.user = {}
        this.User.logined = false;
    }

    @action.bound
    public User_InfoChanger(user: DCUserType){
        this.__ObjectTypeHandler(user,Keys.DCUser);
    }



    /**
     * 
     * @param App
     * 状态数据直接处理模块
     */
    @action.bound
    public App_setConnected(v:boolean){
        this.App.connected = v;
    }

    @action.bound
    public App_setClient(client: any){
        this.App.client = client;
    }

    @action.bound
    public App_setAppLoading(v: boolean){
        this.App.appLoading = v;
    }


    /**
     * 
     * @param data 
     * @param key 
     * 数据处理模块 ，包括对象数据，基本类型数据模块
     * 
     */
    @action.bound
    public DataSettingHandler< T extends DATA >(data: T,key: keyof DataKeys){
        let t = typeof(data);
        console.log("OneForAll::数据中心:: 数据处理器接收到的数据和数据类型为 : ",data,t);
        switch(t){
            case 'object':
                this.__ObjectTypeHandler(data,key);
                break;
            case 'number':
                this.__BaseTypeDataHandler(data);
                break;
            default:
                console.error("Error : 数据类型错误，请检查待处理的数据 ",data);
        }
    }

    private __souceCombine(source,data,keys){
        if(keys.length > 0){
            for(let k of keys){
                if(source[k] != undefined || source[k] != null){
                    source[k] = data[k]
                }else {
                    console.error("Error: 数据中存在目标对象所未拥有的数据类型，key-> ",k," value-> ",data[k]," 所有已注册类型为 : ",Object.keys(source),"传入的数据所有类型: ",IncomeObjKeys);
                }
            }
            return source;
        }
    }

    @action.bound
    public __ObjectTypeHandler<T extends DATA>(data:T,key: keyof DataKeys){
        console.log("OneForAll::数据中心:: <对象类型>数据处理器 接收到的数据和key为",data,key)
        switch(key){
            case Keys.DCAssets:
                let source:DCAssets = this.Assets;
                let IncomeObjKeys = Object.keys(data);
                // REPLACE_TAG 1
                let resultSource = this.__souceCombine(source,data,IncomeObjKeys);
                this.Assets = resultSource;
                break;
            case Keys.DCUser:
                let source_user :DCUserType = this.User.user;
                let IncomeObjKeys_user = Object.keys(data);
                let resultSource_user = this.__souceCombine(source_user,data,IncomeObjKeys_user);
                this.User.user = resultSource_user;
                break;
            //TODO: ADD MORE 
            default:
                console.error("Error: 数据类型未注册，请检查数据中心已注册数据类型");
        }
    }
    @action.bound
    public __BaseTypeDataHandler<T extends DATA>(data:T){
        //处理基本类型数据更新
    }
}

export default new DataCenter();


/**
 *  REPLACETAG 1
 */
// if(IncomeObjKeys.length > 0){
//     for(let k of IncomeObjKeys){
//         if(source[k] != undefined || source[k] != null){
//             source[k] = data[k]
//         }else {
//             console.error("Error: 数据中存在目标对象所未拥有的数据类型，key-> ",k," value-> ",data[k]," 所有已注册类型为 : ",Object.keys(source),"传入的数据所有类型: ",IncomeObjKeys);
//         }
//     }
//     this.Assets = source;
// }
